package com.jhs.boot.system.controller;

import com.jhs.boot.common.annotation.Log;
import com.jhs.boot.common.enums.LogModuleEnum;
import com.jhs.boot.common.result.Result;
import com.jhs.boot.system.model.vo.NodesThirdVO;
import com.jhs.boot.system.service.SysNodesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 节点前端控制层
 *
 * @author zxy
 * @since 2025-08-12 10:33
 */
@Tag(name = "节点实时请求接口")
@RestController
@RequestMapping("/api/v1/wifi")
@RequiredArgsConstructor
public class ApplyNodesController {

    private final SysNodesService sysNodesService;

    @Operation(summary = "节点上线接口")
    @GetMapping("/nodeUpReq")
    public Result getSysNodesForm(
            @Parameter(description = "节点IP",required = true) @RequestParam("ip")  String ip,
            @Parameter(description = "广域网IP",required = false) @RequestParam("wan_ip")  String routeIp,
            @Parameter(description = "节点MAP地址",required = true) @RequestParam("mac")  String mac,
            @Parameter(description = "节点类型",required = true) @RequestParam("type")  Integer type
    ) {
        boolean result =sysNodesService.addNodeUp(ip,type,mac,routeIp);
        return Result.result(result);
    }
    @PreAuthorize("@ss.hasPerm('ap:nodes:reboot')")
    @Operation(summary = "节点重启")
    @Log( value = "节点重启",module = LogModuleEnum.RESET)
    @GetMapping("/reboot")
    public Result reboot(
            @Parameter(description = "节点IP") @RequestParam("ip")  String ip,
            @Parameter(description = "节点类型") @RequestParam("type")  Integer type
    ) {
        boolean result =sysNodesService.reboot(ip ,type);
        return Result.result(result);
    }
    @PreAuthorize("@ss.hasPerm('ap:nodes:config')")
    @Operation(summary = "获取节点信息")
    @GetMapping("/getConfig")
    public Result  getNodeByIp(@Parameter(description = "节点IP") @RequestParam("ip")  String ip,
                                                  @Parameter(description = "节点type") @RequestParam("type")  Integer type,
                                                  @Parameter(description = "配置类的项：system,network") @RequestParam("cfgItem")  String cfgItem) {

        return  sysNodesService.getConfig(ip,cfgItem,type) ;
    }
    @Operation(summary = "保存节点信息")
    @PostMapping("/setConfig")
    @PreAuthorize("@ss.hasPerm('ap:nodes:config:save')")
    @Log( value = "修改服务配置",module = LogModuleEnum.SERVER_SET)
    public Result<Object> setNodeByIp( @RequestBody  NodesThirdVO nodesThirdVO) {
         return Result.success(sysNodesService.updateSet(nodesThirdVO));
    }
    @Operation(summary = "保存节点信息")
    @PostMapping("/test")
    public Result<Object> test( @RequestBody  String nodesThirdVO,
                                @Parameter(description = "节点IP") @RequestParam("ip")  String ip,
                                @Parameter(description = "节点type") @RequestParam("type")  Integer type,
                                @Parameter(description = "配置类的项：system,network") @RequestParam("cfg_item")  String cfgItem) {
        return Result.success();
    }
}
